var mongoose = require("mongoose");
var Rx = require("rxjs");
var operators = require("rxjs/operators");

const globalVar = require("../server-util/globalVar");

/****************************
 * accountBudget
 * 预算
 ****************************/
var accountBudgetProps = {
	budgetMonth: {
		type: String,
		default: "",
	}, //预算日期
	budget: {
		type: String,
		default: "",
	}, //创建日期
	CreateAt: {
		type: String,
		default: "",
	}, //创建日期
	UpdateAt: {
		type: String,
		default: "",
	}, //更新日期
	Remark: {
		type: String,
		default: "",
	}, //备注
	CreatorId: mongoose.Schema.ObjectId, //创建人id
	SchemeId: mongoose.Schema.ObjectId,
};
var accountBudgetSchema = new mongoose.Schema(accountBudgetProps);
var accountBudget = mongoose.model("accountBudget", accountBudgetSchema);
/****************************
 * accountCat
 * 记账类目
 ****************************/
var accountCatProps = {
	AccountType: Boolean, //收入或者支出类型 true 为收入 false 为支出
	TypeName: {
		type: String,
		default: "",
	}, //类目名称
	CreateAt: {
		type: String,
		default: "",
	}, //创建日期
	UpdateAt: {
		type: String,
		default: "",
	}, //更新日期
	OrderNum: {
		type: Number,
		default: 0,
	}, //排序序号
	CreatorId: mongoose.Schema.ObjectId, //创建人id
	SchemeId: mongoose.Schema.ObjectId,
	//收入支出是谁定义的
	DefineType: {
		type: Number,
		default: globalVar.DefineType.isUserDefine,
	},
	//图标地址
	IconPath: {
		type: String,
		default: "",
	},
};
var accountCatSchema = new mongoose.Schema(accountCatProps);
//记账类目
var accountCat = mongoose.model("accountCat", accountCatSchema);

/****************************
 * accountTitle
 * 记账标题
 ****************************/
var accountTitleProps = {
	AccountType: Boolean, //收入或者支出类型 true 为收入 false 为支出
	CatType: {
		type: String,
		default: "",
	}, //关联的记账类目
	CatId: mongoose.Schema.ObjectId, //记账类目id
	TitleName: {
		type: String,
		default: "",
	}, //记账标题名称
	CreateAt: {
		type: String,
		default: "",
	}, //创建日期
	UpdateAt: {
		type: String,
		default: "",
	}, //更新日期
	OrderNum: {
		type: Number,
		default: 0,
	}, //排序序号
	CreatorId: mongoose.Schema.ObjectId, //创建人id
	SchemeId: mongoose.Schema.ObjectId,
	//收入支出是谁定义的
	DefineType: {
		type: Number,
		default: globalVar.DefineType.isUserDefine,
	},
	//图标地址
	IconPath: {
		type: String,
		default: "",
	},
	//首页是否默认选中
	isDefaultSelect: {
		type: Boolean,
		default: false,
	},
};
var accountTitleSchema = new mongoose.Schema(accountTitleProps);
//记账类目
var accountTitle = mongoose.model("accountTitle", accountTitleSchema);

/****************************
 * accountRecord
 * 收支记录
 ****************************/

var accountRecordProps = {
	AccountType: Boolean, //收入或者支出类型 true 为收入 false 为支出
	CatId: mongoose.Schema.ObjectId, //记账类目id
	TitleId: mongoose.Schema.ObjectId, //记账标题id
	AccountRelationPeopleId: mongoose.Schema.ObjectId, //关系人id
	FileIds: {
		type: Object, default: []
	}, //附件id
	//图标地址
	IconPath: {
		type: String,
		default: "",
	},
	TitleName: {
		type: String,
		default: "",
	}, //记账标题名称
	CreateAt: {
		type: String,
		default: "",
	}, //创建日期
	UpdateAt: {
		type: String,
		default: "",
	}, //更新日期
	OrderNum: {
		type: Number,
		default: 0,
	}, //排序序号
	CreatorId: mongoose.Schema.ObjectId, //创建人id
	Place: {
		type: String,
		default: "",
	}, //地点
	Remark: {
		type: String,
		default: "",
	}, //备注
	Money: {
		type: Number,
		default: 0,
	}, //金额
	SchemeId: mongoose.Schema.ObjectId,
};
var accountRecordSchema = new mongoose.Schema(accountRecordProps);

var accountRecord = mongoose.model("accountRecord", accountRecordSchema);






//系统分类或者标题 , 停用记录
var accountCatTitleStopProps = {
	CreatorId: mongoose.Schema.ObjectId, //创建人id
	CatId: mongoose.Schema.ObjectId, //需要停用的catId
	TitleId: mongoose.Schema.ObjectId, //需要停用的titleId
	CreateAt: {
		type: String,
		default: "",
	}, //停用时间
	SchemeId: mongoose.Schema.ObjectId,
	Type: {
		type: String,
		default: globalVar.TypeOfCatOrTitleStop.CAT,
	}, //停用时间
};
var accountCatTitleStopSchema = new mongoose.Schema(accountCatTitleStopProps);

var accountCatTitleStop = mongoose.model(
	"accountCatTitleStop",
	accountCatTitleStopSchema
);

//账户关联人停用记录

var accountRelationProps = {
	CreatorId: mongoose.Schema.ObjectId, //创建人id

	AccountRelationId: mongoose.Schema.ObjectId, //需要停用的accountRelation
	CreateAt: {
		type: String,
		default: "",
	}, //停用时间
	SchemeId: mongoose.Schema.ObjectId
}

var accountRelationSchema = new mongoose.Schema(accountRelationProps);

var accountRelationStop = mongoose.model(
	"accountRelationStop",
	accountRelationSchema
);

//账目关联人员 , 谁用的一笔记录
var accountRelationProps = {
	CreatorId: mongoose.Schema.ObjectId, //创建人id
	CreateAt: {
		type: String,
		default: "",
	}, //停用时间
	SchemeId: mongoose.Schema.ObjectId,
	Title: {
		type: String,
		default: "",
	},
	UpdateAt: {
		type: String,
		default: "",
	}, //更新日期
	//关联人员是谁定义的
	DefineType: {
		type: Number,
		default: globalVar.DefineType.isUserDefine,
	},
	OrderNum: {
		type: Number,
		default: 0,
	}, //排序序号
	Remark: {
		type: String,
		default: "",
	}, //备注
};
var accountRelationSchema = new mongoose.Schema(accountRelationProps);

var accountRelation = mongoose.model("accountRelation", accountRelationSchema);
//初始化账目关联人员
let initAccountRelation = () => {
	accountRelation.find(
		{
			DefineType: globalVar.DefineType.isSysDefault,
		},
		function (ferror, fresult) {
			if (!fresult || (Array.isArray(fresult) && fresult.length < 1)) {
				["自己", "同事", "朋友", "家人"].forEach(function (title) {
					new accountRelation({
						CreateAt: new Date().getTime(),
						DefineType: globalVar.DefineType.isSysDefault,
						title,
					}).save(function (serror, sresult) {
						if (serror) {
							throw Error(serror);
						} else {
							console.log("账目关联人员初始化完成!", sresult);
						}
					});
				});
			} else {
				console.log("accountRelation已存在!");
			}
		}
	);
};
initAccountRelation();

//自动初始化系统分类和标题
let initSysAccountCatAndTitle = ({
	catTypeName,
	AccountType,
	titleArr,
	iconArr,
	catIcon,
}) => {
	new Rx.Observable((ob) => {
		accountCat.find(
			{
				TypeName: catTypeName,
				DefineType: globalVar.DefineType.isSysDefault,
			},
			function (ferror, fresult) {
				//没有默认分类，新建
				if (!fresult || (Array.isArray(fresult) && fresult.length < 1)) {
					new accountCat({
						TypeName: catTypeName,
						CreateAt: new Date().getTime(),
						OrderNum: 0,
						AccountType,
						DefineType: globalVar.DefineType.isSysDefault,
						IconPath: catIcon,
					}).save(function (serror, sresult) {
						if (!serror) {
							ob.next({
								data: sresult,
								bool: true,
								msg: null,
							});
							ob.complete();
						} else {
							ob.error({
								data: null,
								bool: false,
								msg: serror,
							});
							ob.complete();
						}
					});
				}
				//有默认分类
				else {
					ob.next({
						data: null,
						bool: true,
						msg: null,
					});
					ob.complete();
				}
			}
		);
	})
		.pipe(
			operators.mergeMap(({ data, bool, msg }) => {
				return new Rx.Observable((ob) => {
					//有错误
					if (!bool && msg && !data) {
						ob.error(msg);
						ob.complete();
					}
					//没错误，但是已经建了默认分类
					else if (bool && !msg && !data) {
						ob.next();
						ob.complete();
					}
					//没错误，还没建默认分类
					else {
						let parr = [];
						titleArr.forEach((feRes, index) => {
							parr.push(
								new Promise((resolve, reject) => {
									let isDefaultSelect = false;
									//默认选中工资薪水
									if (feRes == "工资薪水" || feRes == "餐饮其他") {
										isDefaultSelect = true;
									}
									new accountTitle({
										CatType: data.TypeName,
										TitleName: feRes,
										CreateAt: new Date().getTime(),
										AccountType,
										CatId: data._id,
										DefineType: globalVar.DefineType.isSysDefault,
										IconPath: iconArr[index],
										isDefaultSelect,
									}).save((serr, sresult) => {
										if (!serr) {
											resolve(sresult);
										} else {
											reject(serr);
										}
									});
								})
							);
						});
						Promise.all(parr)
							.then((res) => {
								ob.next(res);
								ob.complete();
							})
							.catch((error) => {
								ob.error(error);
								ob.complete();
							});
					}
				});
			})
		)
		.subscribe((res) => { });
};

initSysAccountCatAndTitle({
	catIcon: "color_svg_294.svg",
	catTypeName: "全部",
	AccountType: true,
	titleArr: [
		"工资薪水",
		"利息",
		"兼职外快",
		"营业收入",
		"红包",
		"销售款",
		"退款返款",
		"福利补贴",
		"报销款",
		"余额宝",
		"应收款",
		"奖金",
		"生活费",
		"基金",
		"分红",
		"股票",
		"租金",
		"公积金",
		"工程款",
		"赔付款",
		"漏记款",
		"其他",
	],
	iconArr: [
		"color_svg_121.svg",
		"color_svg_94.svg",
		"color_svg_51.svg",
		"color_svg_26.svg",
		"color_svg_115.svg",
		"color_svg_55.svg",
		"color_svg_77.svg",
		"color_svg_79.svg",
		"color_svg_83.svg",
		"color_svg_107.svg",
		"color_svg_109.svg",
		"color_svg_112.svg",
		"color_svg_121.svg",
		"color_svg_226.svg",
		"color_svg_115.svg",
		"color_svg_153.svg",
		"color_svg_157.svg",
		"color_svg_173.svg",
		"color_svg_247.svg",
		"color_svg_177.svg",
		"color_svg_180.svg",
		"color_svg_181.svg",
	],
});
initSysAccountCatAndTitle({
	catIcon: "color_svg_12.svg",
	catTypeName: "生意",
	AccountType: false,
	titleArr: [
		"进货采购",
		"材料辅料",
		"工资支出",
		"办公费用",
		"交通运输",
		"工程款",
		"运营费",
		"会务费",
		"营销广告",
		"店面租金",
		"注册登记",
		"生意其他",
	],
	iconArr: [
		"color_svg_339.svg",
		"color_svg_289.svg",
		"color_svg_346.svg",
		"color_svg_331.svg",
		"color_svg_171.svg",
		"color_svg_290.svg",
		"color_svg_273.svg",
		"color_svg_299.svg",
		"color_svg_365.svg",
		"color_svg_263.svg",
		"color_svg_272.svg",
		"color_svg_286.svg",
	],
});
initSysAccountCatAndTitle({
	catIcon: "color_svg_256.svg",
	catTypeName: "人情",
	AccountType: false,
	titleArr: [
		"礼金红包",
		"物品",
		"请客",
		"孝敬",
		"代付款",
		"给予",
		"捐款",
		"人情其他",
	],
	iconArr: [
		"color_svg_166.svg",
		"color_svg_184.svg",
		"color_svg_256.svg",
		"color_svg_34.svg",
		"color_svg_226.svg",
		"color_svg_85.svg",
		"color_svg_97.svg",
		"color_svg_152.svg",
	],
});
initSysAccountCatAndTitle({
	catIcon: "color_svg_94.svg",
	catTypeName: "投资",
	AccountType: false,
	titleArr: [
		"p2p",
		"余额宝",
		"理财产品",
		"投资贷款",
		"银行存款",
		"证券期货",
		"外汇",
		"贵金属",
		"收藏品",
		"投资其他",
	],
	iconArr: [
		"color_svg_112.svg",
		"color_svg_60.svg",
		"color_svg_58.svg",
		"color_svg_164.svg",
		"color_svg_289.svg",
		"color_svg_157.svg",
		"color_svg_339.svg",
		"color_svg_137.svg",
		"color_svg_197.svg",
		"color_svg_288.svg",
	],
});

initSysAccountCatAndTitle({
	catIcon: "color_svg_129.svg",
	catTypeName: "居家",
	AccountType: false,
	titleArr: [
		"美发美容",
		"住宿房租",
		"房款贷款",
		"材料建材",
		"快递邮费",
		"电脑宽带",
		"家政服务",
		"物业",
		"水费手续费",
		"保险费",
		"消费贷款",
		"婚庆摄影",
		"生活其他",
		"漏记款",
	],
	iconArr: [
		"color_svg_46.svg",
		"color_svg_198.svg",
		"color_svg_273.svg",
		"color_svg_15.svg",
		"color_svg_84.svg",
		"color_svg_229.svg",
		"color_svg_283.svg",
		"color_svg_168.svg",
		"color_svg_248.svg",
		"color_svg_69.svg",
		"color_svg_281.svg",
		"color_svg_91.svg",
		"color_svg_95.svg",
		"color_svg_193.svg",
	],
});

initSysAccountCatAndTitle({
	catIcon: "color_svg_110.svg",
	catTypeName: "医教",
	AccountType: false,
	titleArr: [
		"住院费",
		"养老院",
		"学杂教材",
		"培训考试",
		"幼儿教育",
		"学费",
		"出国留学",
		"家教补习",
		"医教其他",
		"助学贷款",
		"助学贷款",
	],
	iconArr: [
		"color_svg_111.svg",
		"color_svg_299.svg",
		"color_svg_194.svg",
		"color_svg_202.svg",
		"color_svg_34.svg",
		"color_svg_202.svg",
		"color_svg_344.svg",
		"color_svg_322.svg",
		"color_svg_251.svg",
		"color_svg_313.svg",
		"color_svg_104.svg",
	],
});

initSysAccountCatAndTitle({
	catIcon: "color_svg_35.svg",
	catTypeName: "娱乐",
	AccountType: false,
	titleArr: [
		"麻将其他",
		"洗浴足浴",
		"花鸟宠物",
		"聚会玩乐",
		"运动健身",
		"茶酒咖啡",
		"卡拉OK",
		"电视",
		"歌舞演出",
		"娱乐其他",
	],
	iconArr: [
		"color_svg_88.svg",
		"color_svg_87.svg",
		"color_svg_333.svg",
		"color_svg_38.svg",
		"color_svg_41.svg",
		"color_svg_337.svg",
		"color_svg_35.svg",
		"color_svg_335.svg",
		"color_svg_146.svg",
		"color_svg_87.svg",
	],
});

initSysAccountCatAndTitle({
	catIcon: "color_svg_19.svg",
	catTypeName: "购物",
	AccountType: false,
	titleArr: [
		"烟酒",
		"电子数码",
		"文具玩具",
		"报刊书籍",
		"珠宝首饰",
		"家居家纺",
		"保健用品",
		"印刷",
		"购物其他",
		"电器",
	],
	iconArr: [
		"color_svg_103.svg",
		"color_svg_300.svg",
		"color_svg_155.svg",
		"color_svg_110.svg",
		"color_svg_127.svg",
		"color_svg_129.svg",
		"color_svg_188.svg",
		"color_svg_194.svg",
		"color_svg_166.svg",
		"color_svg_199.svg",
	],
});

initSysAccountCatAndTitle({
	catIcon: "color_svg_9.svg",
	catTypeName: "交通",
	AccountType: false,
	titleArr: [
		"打车",
		"公交",
		"加油",
		"停车费",
		"地铁",
		"火车",
		"长途汽车",
		"自行车",
		"飞机",
		"保养维修",
		"过路过桥",
		"船",
		"车款车贷",
		"车险",
		"罚款赔偿",
		"驾照费用",
		"交通其他",
	],
	iconArr: [
		"color_svg_341.svg",
		"color_svg_167.svg",
		"color_svg_162.svg",
		"color_svg_163.svg",
		"color_svg_11.svg",
		"color_svg_11.svg",
		"color_svg_278.svg",
		"color_svg_174.svg",
		"color_svg_344.svg",
		"color_svg_302.svg",
		"color_svg_249.svg",
		"color_svg_179.svg",
		"color_svg_123.svg",
		"color_svg_170.svg",
		"color_svg_138.svg",
		"color_svg_276.svg",
		"color_svg_156.svg",
	],
});

initSysAccountCatAndTitle({
	catIcon: "color_svg_28.svg",
	catTypeName: "餐饮",
	AccountType: false,
	titleArr: [
		"早餐",
		"午餐",
		"晚餐",
		"夜宵",
		"零食",
		"饮料水果",
		"买菜原料",
		"油盐酱醋",
		"餐饮其他",
	],
	iconArr: [
		"color_svg_28.svg",
		"color_svg_28.svg",
		"color_svg_28.svg",
		"color_svg_28.svg",
		"color_svg_23.svg",
		"color_svg_32.svg",
		"color_svg_65.svg",
		"color_svg_31.svg",
		"color_svg_28.svg",
	],
});

module.exports = {
	accountCat,
	accountTitle,
	accountRecord,
	accountBudget,
	accountCatTitleStop,
	accountRelation,
	accountRelationStop
};
